<?php

class UserAccessComponent extends Component {
    private $_excludedControllers = array('AppController', 'PagesController');
    
    var $components = array('Auth');
    public function getListController() {
        $controllerClasses = App::objects("controller");
        $controllers = array();
        //pr($controllerClasses);die;
        foreach($controllerClasses as $controller) {
            if (!in_array($controller, $this->_excludedControllers)) {
                App::uses($controller,'Controller');
                $methods = get_class_methods($controller);
                $control = str_replace("Controller", "",$controller);
                $return = array();
                if(!empty($methods)){
                    foreach($methods as $method) {
                        $arr = explode("admin", $method);
                        if(isset($arr[1])){
                            $method = str_replace("admin_", "", $method);
                            switch ($method) {
                                case 'index':
                                    $return[$method]='View List '.$control;
                                    break;
                                case 'add':
                                    $return[$method]='Add new '.$control;
                                    break;
                                case 'edit':
                                    $return[$method]='Edit '.$control;
                                    break;
                                case 'delete':
                                    $return[$method]='Delete '.$control;
                                    break;
                                case 'changepass':
                                    $return[$method]='Change password for user';
                                    break;
                                case 'notification_template':
                                    $return[$method]='View the notification template';
                                    break;
                                case 'template_edit':
                                    $return[$method]='Edit notification template';
                                    break;
                                case 'group':
                                    $return[$method]='View list of group';
                                    break;
                                case 'group_add':
                                    $return[$method]='Add new group';
                                    break;
                                case 'group_edit':
                                    $return[$method]='Edit a group';
                                    break;
                                case 'group_delete':
                                    $return[$method]='Delete a group';
                                    break;
                                case "edit_type":
                                    $return[$method]='Edit type of a customer';
                                    break;
                                case "edit_group":
                                    $return[$method]='Edit group of a customer';
                                    break;
                                case "change_status":
                                    $return[$method]='Change status of a '.$control;
                                    break;
                                case "reply":
                                    $return[$method]="Reply ticket messages";
                                    break;
                            }
                       }
                    }
                }
                if(!empty($return)){
                    $controllers[$controller] = $return;
                }
            }
        }
        //pr($controllers);die;
        return $controllers;
    }
    
    public function get_user_permission(){
        $user = ClassRegistry::init('User');
        $group = ClassRegistry::init('Group');
        $permission = array();
        $gr = $group->findById($this->Auth->user('group_id'));
        $permission = json_decode($gr['Group']['data'],true);
        return $permission;
    }
    public function get_user_permission_list(){
        $user = ClassRegistry::init('User');
        $group = ClassRegistry::init('Group');
        $permission = array();
        $gr = $group->findById($this->Auth->user('group_id'));
        $permission = json_decode($gr['Group']['data'],true);
        $return = array();
        foreach($permission as $controller=>$actions){
            $new_controller = Inflector::tableize($controller);
            $return[$new_controller]=$actions;
        }
        return $return;
    }
    public function get_first_redirect(){
        $permissions = $this->get_user_permission();
        $user = ClassRegistry::init('User');
        $return = array();
        if(!empty($permissions)){
            $permission = array_slice($permissions, 0,1);
            $controller='';
            $action = '';
            $prefix = '';
            foreach($permission as $control=>$actions){
                $controller=$control;
                $prefix = 'admin';
                foreach($actions as $act){
                    $action = $act;
                }
                if(in_array('index',$actions)){
                    $action = 'index';
                }
            }
            $controller = Inflector::tableize($controller);
            $return = array('controller'=>$controller,'action'=>$action,$prefix=>true);
        }
        return $return;
    }
    public function check_access($controller,$action){
        $permissions = $this->get_user_permission();
        //pr($permissions);die;
        $check = 0;
        foreach ($permissions as $control => $acts) {
            $control = str_replace("Controller", "", $control);
            $control = Inflector::tableize($control);
            
            if($control == $controller){
                foreach ($acts as $act) {
                    if($act == $action){
                        $check=1;
                    }
                }
            }
        }
        return $check;
    }
    public function get_admin_permission(){
        $return = array(); 
        $controllers = $this->getListController();
        foreach($controllers as $controller=>$actions){
            if(!empty($actions)){
                foreach($actions as $action=>$description){
                    $return[$controller][]=$action;
                }
            }
        }
        return $return;
    }
}